package com.shi.yibanjia.controller;

import com.shi.yibanjia.common.Result;
import com.shi.yibanjia.exception.BusinessException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

@RestControllerAdvice
public class GlobalExceptionController {

    @ExceptionHandler(Exception.class)
    public Result handle(Exception e) {
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result handle(RuntimeException e) {
        e.printStackTrace();
        return Result.fail("服务器正在升级，请稍后重试");
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Result handle(MaxUploadSizeExceededException e) {
        e.printStackTrace();
        return Result.fail("文件大小超出限制");
    }

    @ExceptionHandler(BusinessException.class)
    public Result handle(BusinessException e) {
        return Result.fail(e);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handle(MethodArgumentNotValidException e) {
        return Result.fail(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }
}
